package com.vankimgps.listvehicles;

import java.util.ArrayList;
import java.util.List;

import com.vankimgps.activity.R;
import com.vankimgps.models.VehicleStatus;
import com.vankimgps.models.Vehicles;

import android.app.Activity;
import android.content.Context;
import android.content.res.ColorStateList;
import android.graphics.Color;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.TextView;

public class ListVehiclesAdapter extends ArrayAdapter<VehicleStatus> {
	Context context;
    int layoutResourceId;    
	List<VehicleStatus> listVehicles;

	public ListVehiclesAdapter(Context context, int textViewResourceId,
			ArrayList<VehicleStatus> objects) {
		super(context, textViewResourceId, objects);
		layoutResourceId = textViewResourceId;
		this.listVehicles = objects;
		this.context = context;	
		Log.i("VANKIM","VANKIM : ListVehiclesAdapter");
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {

		View row = convertView;
		
		if (row == null){
            LayoutInflater inflater = ((Activity)context).getLayoutInflater();
            row = inflater.inflate(layoutResourceId, parent, false);
		}

		TextView  txtVehicleNo =  (TextView) row.findViewById(R.id.txtVehicleNo);
		TextView  txtSpeed =  (TextView) row.findViewById(R.id.txtSpeed);
		TextView  txtDateTime =  (TextView) row.findViewById(R.id.txtDateTime);
		TextView  txtGas =  (TextView) row.findViewById(R.id.txtGas);
		VehicleStatus vehicle= listVehicles.get(position);
		
		if (vehicle.getVelocity() > 0)
			txtSpeed.setTextColor(Color.GREEN);
		else 
			txtSpeed.setTextColor(Color.RED);
		txtVehicleNo.setText(vehicle.getVehNoF());
		txtSpeed.setText(Integer.toString(vehicle.getVelocity()) + " km/h");
		txtDateTime.setText(vehicle.getTime());
		txtGas.setText(Double.toString(vehicle.getOil() / 100.0) + "lit");

		return row;
	}
}
